//
//  HRStatusOriginalView.swift
//  microBlog
//
//  Created by HR_W on 16/5/24.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let margin: CGFloat = 8

class HRStatusOriginalView: UIView {
    
    var bottomCons: Constraint?
    var statusViewModel: HRStatusViewModel? {
    
        didSet {
        
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            
            nameLabel.text = statusViewModel?.status?.user?.name
            
            timeLabel.text = statusViewModel?.time
            
            sourceLabel.text = statusViewModel?.sourceText
            
            contentLabel.text = statusViewModel?.status?.text
        
           verifiedImageView.image = statusViewModel?.veritiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage
           
            //pictureView.pictures = statusViewModel?.status?.pic_urls
            
            bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                pictureView.pictures = urls
                
                self.snp_updateConstraints(closure: { (make) in
                    
                   self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
                })
                pictureView.hidden = false
            }else{
                pictureView.pictures = nil
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
                })
            
                pictureView.hidden = true
            }
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //self.backgroundColor = UIColor.yellowColor()
        
        setupUI()
        
    }
    
    private func setupUI(){
    
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(mbrankImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        addSubview(sepView)
        sepView.backgroundColor = UIColor.yellowColor()
        
        sepView.snp_makeConstraints { (make) in
            
            make.top.left.right.equalTo(self)
            make.height.equalTo(margin)
        }
        
        
       headIcon.snp_makeConstraints { (make) in
        
       // make.left.top.equalTo(self).offset(margin)
       
        make.top.equalTo(sepView.snp_bottom).offset(margin)
        make.left.equalTo(self).offset(margin)
        
        make.height.width.equalTo(40)
        
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            
            make.right.bottom.equalTo(headIcon)
        }
        
       nameLabel.snp_makeConstraints { (make) in
        
            make.left.equalTo(headIcon.snp_right).offset(margin)
            make.top.equalTo(headIcon.snp_top)
        }
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(headIcon.snp_bottom)
            make.left.equalTo(headIcon.snp_right).offset(margin)
        }
    
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(margin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_left)
            make.top.equalTo(headIcon.snp_bottom).offset(margin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.left.equalTo(contentLabel.snp_left)
            
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) in
            
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private lazy var sepView: UIView = UIView()
    
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var timeLabel: UILabel = UILabel(titie: "12:12", font: 10, textColor: UIColor.orangeColor())
    
    private lazy var sourceLabel: UILabel = UILabel(titie: "爱玩网", font: 10, textColor: UIColor.darkGrayColor())
    
    private lazy var nameLabel: UILabel = UILabel(titie: "安程", font: 14, textColor: UIColor.darkGrayColor())
    
    private lazy var contentLabel: UILabel = UILabel(titie: "我是内容部分我是内容部分我是内容部分我是内容部分我是内容部分", font: 14, textColor: UIColor.darkGrayColor(),alignment: .Left)
    
    private lazy var pictureView: HRStatusPictureView = HRStatusPictureView()

}
